package database;

import item.ShipItem;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShipItemTable extends Table {

	private static final String SHIP_ITEM_INSERT = "INSERT INTO ShipItem VALUES (%s, %s, %s, %s)";
	private static final String SHIP_ITEM_DELETE = "DELETE FROM ShipItem WHERE sid=%s AND upc=%s";
	private static final String SHIP_ITEM_DISPLAY = "SELECT * FROM ShipItem";
	
	private static final String SHIP_ITEM_FROM_SID_QUERY = "SELECT * FROM ShipItem WHERE sid=%d";	
	private static final String SHIP_ITEM_EXISTS_QUERY = "SELECT * FROM ShipItem WHERE sid=%s and upc=%s";
	
	public enum Column
	{
		SID(1, true),
		UPC(2, true),
		SUP_PRICE(3, false),
		QUANTITY(4, false);
		
		private int index;
		private boolean isPrimaryKey;
		
		private String[] columnNames = {
				"sid",
				"upc",
				"supPrice",
				"quantity"};
		
		private Column(int index, boolean isPrimaryKey) 
		{
			this.index = index;
			this.isPrimaryKey = isPrimaryKey;
		}
		
		public int getIndex()
		{
			return index;
		}
		
		public String toString()
		{
			return columnNames[this.ordinal()];
		}

		public boolean isPrimaryKey() {
			return this.isPrimaryKey;
		}
	};
	
	public ShipItemTable()
	{
		super(SHIP_ITEM_INSERT, SHIP_ITEM_DELETE, SHIP_ITEM_DISPLAY, SHIP_ITEM_EXISTS_QUERY);
	}
	
	public List<ShipItem> getShipItemsFromSid(int sid) throws SQLException
	{
		List<ShipItem> shipItems = new ArrayList<ShipItem>();
		String fmtdQuery = String.format(SHIP_ITEM_FROM_SID_QUERY, sid);
		
		ResultSet rs = dbCon.executeQuery(fmtdQuery);
		if(rs != null)
		{
			while(rs.next())
			{
							
				String curUpc = rs.getString(Column.UPC.getIndex());
				float supPrice = rs.getFloat(Column.SUP_PRICE.getIndex());
				int quantity = rs.getInt(Column.QUANTITY.getIndex());
								
				ShipItem curShipItem = new ShipItem(sid, supPrice, quantity, curUpc);
				shipItems.add(curShipItem);
			}
			
			rs.close();
		}
		
		return shipItems;
	}
	
	public void removeShipItemEntry(int sid, String upc) throws SQLException
	{
		String fmtdStatement = String.format(SHIP_ITEM_DELETE, String.valueOf(sid), upc);
		dbCon.executeStatement(fmtdStatement);
	}

	@Override
	public List<String> getPrimaryKeyValues(List<String> insertValues) {
		List<String> primaryKeyValues = new ArrayList<String>();
		for(Column col : Column.values())
		{
			if(col.isPrimaryKey())
			{	
				primaryKeyValues.add(insertValues.get(col.ordinal()));
			}
		}				
		
		return primaryKeyValues;
	}
	
	@Override
	public List<String> getColumnNames() {
		Column columns[] = Column.values();
		
		List<String> columnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			columnNames.add(col.ordinal(), col.toString());
		}
		return columnNames;
	}

	@Override
	public List<String> getPrimaryKeyColumnNames() {
		Column columns[] = Column.values();
		
		List<String> primaryKeyColumnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			if(col.isPrimaryKey())
			{
				primaryKeyColumnNames.add(col.ordinal(), col.toString());
			}
		}
		return primaryKeyColumnNames;
	}
	
}
